<?php
/**************************************
 *
 * @Repositories 数据层~主要与数据库打交道
 *
 **************************************/
namespace app\common\repositories;

use app\common\model\UserModel;

use app\common\model\CalendarModel;

use think\Model;

use  think\Cache;

use think\db;


class FligtRepositories
{
    protected $UserModel;
    protected $CalendarModel;
    // 修改用户信息自动过滤字段
    protected $field = ['mood', 'img', 'update_time', 'label', 'is_arti'];
    //点赞自动筛选字段
    protected $clickFile = ['click'];

    protected $cache_time;

    public function __construct()
    {
        $this->cache_time = 3600*24;
        $this->UserModel = new UserModel();
        $this->CalendarModel = new CalendarModel();
    }

    public function regist($data, $request_data)
    {
        $wx_session = $this->generate3rdSession($data['openid']);
        $user = $this->UserModel->where('openid', $data['openid'])->find();
        if (!empty($user)) {
            Cache::set($wx_session, $user, $this->cache_time);
            return $wx_session;

        } else {
            $data['openid'] = $data['openid'];
            $data['nickname'] = $request_data['nickname'];
            $data['headimgurl'] = $request_data['headimgurl'];
            if (isset($data['unionid'])) {
                $data['unionid'];
            }
            $data['addtime'] = time();
            $checkData = $this->UserModel->checkData($data);

            if ($checkData != 'success') {
                return $checkData;
            }

            $res = $this->UserModel->allowField(['openid','unionid','addtime', 'nickname', 'headimgurl'])->save($data);

            if (is_null($res)) {
                return false;
            }
            Cache::set($wx_session, $user, 3600*24);
            return $wx_session;
        }
    }

    /**
     * 生成3rd_session
     *
     * @param $openid
     * @return string
     */
    private function generate3rdSession($openid)
    {
        return md5(mt_rand() . $openid);
    }

    //添加订单
    public function addOrder($data)
    {
        $data['create_time'] = time();
        $rsl = Db::name('order')->insertGetId($data);
        if ($rsl) {
            return $rsl;
        }
        return false;

    }

    //用户主页
    public function userInfo($param)
    {
        $user_id = null;
        if ($param) {
            $user_id = $param['user_id'];
        }
        $clickRepositories = new ClickRepositories();
        //最近浏览文章数
        $viewArt = $clickRepositories->getViewArt($user_id);
        $viewCount = !$viewArt ? 0 : count(array_unique($viewArt));

        //点赞的文章
        $clickNum = $clickRepositories->getClickAtr($user_id);
        $clickCount = !$clickNum ? 0 :count($clickNum);

        //关注几篇专题
        $followNum = $clickRepositories->getFollowSp($user_id);
//        print_r($followNum);exit();
        $followCount = !$followNum ? 0 :count($followNum);
        return ['viewCount' => $viewCount, 'clickCount' => $clickCount, 'followCount' => $followCount];
    }

    //近期阅读
    public function nearRead($param)
    {
        $clickRepositories = new ClickRepositories();
        $viewArt = $clickRepositories->getViewArt($param['user_id']);
        if (!$viewArt) {
            return false;
        }
//        print_r($viewArt);exit();
        $artId = array_unique($viewArt);
        foreach ($artId as $vo) {
            $arr[] = Db::table('article')->where(['id' => $vo,'status' => 1])->find();
        }
        foreach ($arr as $k => $v) {
            if (empty($v)) {
                unset($arr[$k]);
            } else {
                $item[] = $v;
            }
        }
        if (!$arr) {
            return false;
        }
        //用户点赞数
        $clickRepositories = new ClickRepositories();
        for ($i=0; $i<count($item);$i++) {
            $data[$i]['id'] = $item[$i]['id'];
            $data[$i]['user_id'] = $param['user_id'];
        }
        $clickNum = $clickRepositories->clickNum($data);
        //重组数组
        if ($clickNum) {
            for ($i=0; $i<count($item);$i++) {
                $item[$i]['click'] = $clickNum[$i]['click'];
                $item[$i]['whether'] = $clickNum[$i]['whether'];
            }
        }
        //最新点赞的三个头像
        $newImg = $clickRepositories->getClickImg($data);
        for ($i=0; $i<count($item);$i++) {
            $item[$i]['newImg'] = $newImg[$i]['click'];
        }
        return $item;
    }

    //我喜欢的文章
    public function likeAtr($param)
    {
        $clickRepositories = new ClickRepositories();
        $clickNum = $clickRepositories->getClickAtr($param['user_id']);
        if (!$clickNum) {
            return false;
        }
        $clickArr = array_values($clickNum);
        foreach ($clickArr as $vo) {
            $arr[] = Db::table('article')->where(['id' => $vo])->find();
        }
        foreach ($arr as $k => $v) {
            if (empty($v)) {
                unset($arr[$k]);
            } else {
                $item[] = $v;
            }
        }
        if (!$arr) {
            return false;
        }
        //用户点赞数
        $clickRepositories = new ClickRepositories();
        for ($i=0; $i<count($item);$i++) {
            $data[$i]['id'] = $item[$i]['id'];
            $data[$i]['user_id'] = $param['user_id'];
        }
        $clickNum = $clickRepositories->clickNum($data);
        //重组数组
        if ($clickNum) {
            for ($i=0; $i<count($item);$i++) {
                $item[$i]['click'] = $clickNum[$i]['click'];
                $item[$i]['whether'] = $clickNum[$i]['whether'];
            }
        }
        //最新点赞的三个头像
        $newImg = $clickRepositories->getClickImg($data);
        for ($i=0; $i<count($item);$i++) {
            $item[$i]['newImg'] = $newImg[$i]['click'];
        }
        return $item;
    }

    //用户点赞功能
    public function clickDaily($param)
    {
        $checkData = $this->CalendarModel->unset_data($param, $this->clickFile);
        if ($param['click'] == 1) {
            $data['click'] = -1;
        } elseif ($param['click'] == -1) {
            $data['click'] = 1;
        }
        $msg = $this->CalendarModel->where('id', $param['id'])->update($data);
        if ($msg) {
            return ['whether' => $data['click']];
        } else {
            return 400;
        }
    }


    //获取redis点赞量
    public function clickNum($data)
    {
        foreach ($data as $k => &$v) {
            $v['click'] = $this->RedisServices->getClick($v['id']);
            $v['whether'] = $this->RedisServices->isClick($v['id'], $v['user_id']);
        }
        return $data;
    }
}